package leetcode.list;

import utils.node.ListNode;

/**
 * DESCRIPTION:
 * Author: ammar
 * Date:   2021/12/10
 * Time:   上午7:42
 */
public class Tools {
    /**
     * 根据整形数组返回链表
     * @return
     */
    public static ListNode generList(Integer[] integers) {
        if (integers.length == 0) return null;
        ListNode node = null;
        ListNode temp = null;
        for (Integer integer : integers) {
            ListNode listNode = new ListNode(integer);
            if (node == null) {
                node = listNode;
                temp = listNode;
                continue;
            }
            temp.next = listNode;
            temp = listNode;
        }
        return node;
    }

    /**
     * 1,2 正常的链表
     * 3,4,5 循环部分的链表
     * 根据数组生成一个环形链表
     */
    public static ListNode generCircleList(Integer[] integers, Integer[] circleEle) {
        if (integers.length == 0 || circleEle.length == 0) throw new RuntimeException("数组不能为空");
        ListNode node = null;
        ListNode head = null;
        for (Integer integer : integers) {
            ListNode listNode = new ListNode(integer);
            if (node == null) {
                node = listNode;
                head = listNode;
                continue;
            }
            head.next = listNode;
            head = listNode;
        }
        ListNode temp = head;
        for (Integer integer : circleEle) {
            ListNode listNode = new ListNode(integer);
            head.next = listNode;
            head = listNode;
        }
        head.next = temp.next;
        return node;
    }
}
